const defaultState = {
  initData: [], // 初始化的所有的数据
  cartData: [], // 加入购物车中的数据
  swiperData: []
}


export default (state = defaultState, action) => {
 // 拷贝数据  修改数据  返回确定数据
 const newState = JSON.parse(JSON.stringify(state));
 if (action.type === 'init_cart_data') {
   newState.initData = action.data;
  //  console.log(newState, 'reducer----da')
 }

 if (action.type === 'to_cart') {
   // 1. 去重
   let goods = newState.cartData.find(val => val.id === action.item.id);
   if (goods) {
     goods.count++;
   } else {
     action.item.count = 1;
     newState.cartData.push(action.item)
   }
 }
 
 // ++
 if (action.type === 'add_cart_item') {
   newState.cartData[action.index].count++;
 }

 // --
 if (action.type === 'remove_cart_item') {
   if (newState.cartData[action.index].count <= 1) {
     if (window.confirm('确定删除吗')) {
       newState.cartData.splice(action.index, 1)
       return newState;
     }
   } else {
    newState.cartData[action.index].count--;
   }
  }

  if(action.type === 'init_banner_img') {
    newState.swiperData = action.data
  }

 return newState; 
}
